home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DOSREF33.ZIP / CHAPTER.006 < prev    next >
Text File  |  1994-01-20  |  34KB  |  602 lines

  1.  
  2.        **  Programmer's Technical Reference for MSDOS and the IBM PC **
  3.                 USA copyright TXG 392-616  ALL RIGHTS RESERVED
  4.      ──────────────────────────┤ DOSREF (tm) ├───────────────────────────
  5.                      ISBN 1-878830-02-3 (disk-based text)
  6.                     Copyright (c) 1987, 1994 Dave Williams
  7.                         ┌─────────────────────────────┐
  8.                         │ Shareware Version, 01/20/94 │
  9.                         │  Please Register Your Copy  │
  10.                         └─────────────────────────────┘
  11.  
  12.                              C H A P T E R   S I X
  13.  
  14.                        DOS CONTROL BLOCKS AND WORK AREAS
  15.  
  16.  
  17.  Contrary to popular belief, DOS is not limited to 640k of work space. This 
  18. constraint is enforced by the mapping of ROM and video RAM into the default 1 
  19. megabyte CPU address space. Some MSDOS compatible machines, such as the Sanyo 
  20. 55x series, can have as much as 768k of contiguous DOS workspace with the 
  21. appropriate option boards. Since DOS has no real memory management, it cannot 
  22. deal with a fragmented workspace. Fragmented RAM (such as RAM mapped into the 
  23. option ROM address space) can be dealt with as a RAMdisk or other storage area 
  24. by using a device driver or other software.
  25.  
  26.  The 80386 CPU and appropriate control software can create a DOS workspace of 
  27. more than one megabyte. Certain add-on boards can also add more than a 
  28. megabyte of workspace, but only for specially written software. Since these 
  29. are all proprietary schemes, little information is availible at present.
  30.  
  31.  When DOS loads a program, it first sets aside a section of memory for the 
  32. program called the program segment, or code segment. Then it constructs a 
  33. control block called the program segment prefix, or PSP, in the first 256 
  34. (100h) bytes. Usually, the program is loaded directly after the PSP at 100h.
  35.  The PSP contains various information used by DOS to help run the program.  
  36. The PSP is always located at offset 0 within the code segment. When a program 
  37. recieves control certain registers are set to point to the PSP. For a COM 
  38. file, all registers are set to point to the beginning of the PSP and the 
  39. program begins at 100h. For the more complex EXE file structures, only DS and 
  40. ES registers are set to point to the PSP. The linker passes the settings for 
  41. the DS, IP, SS, and SP registers and may set the starting location in CS:IP to 
  42. a location other than 100h.
  43.  
  44.  IBMBIO provides an IRET instruction at absolute address 847h for use as a 
  45. dummy routine for interrupts that are not used by DOS. This lets the interrupts
  46. do nothing until their vectors are rerouted to their appropriate handlers.
  47.  
  48.  A storage block is used by DOS to record the amount and location of allocated 
  49. memory within the machine's address space.
  50.  A storage block, a Program Segment Prefix, and an environment area are built 
  51. by DOS for each program currently resident in the address space. The storage 
  52. block is used by DOS to record the address range of memory allocated to a 
  53. program. It is used by DOS to find the next availible area to load a program 
  54. and to determine if there is enough memory to run that porogram. When a 
  55. memory area is in use, it is said to be allocated. Then the program ends, or 
  56. releases memory, it is said to be deallocated. 
  57.  A storage block contains a pointer to the Program Segment Prefix associated 
  58. with each program. This control block is constructed by IBMDOS for the purpose 
  59. of providing standardized areas for DOS/program communication. Within the 
  60. PSP are areas which  are used to save interrupt vectors, pass parameters to 
  61. the program, record disk directory information, and to buffer disk reads and 
  62. writes. This control block is 100h bytes in length and is followed by the 
  63. program module loaded by DOS. 
  64.  The PSP contains a pointer to the environment area for that program. This 
  65. area contains a copy of the current DOS SET, PROMPT, COMSPEC, and PATH values 
  66. as well as any user-set variables. The program may examine and modify this 
  67. information as desired. 
  68.  Each storage block is 10h bytes long, although only 5 bytes are currently 
  69. used by DOS. The first byte contains 4Dh (a capital M) to indicate that it 
  70. contains a pointer to the next storage block. A 5Ah (a capital Z) in the 
  71. first byte of a storage block indicatres there are no more storage blocks 
  72. following this one (it is the end of the chain). The identifier byte is 
  73. followed by a 2 byte segment number for the associated PSP for that program. 
  74. The next 2 bytes contain the number of segments what are allocated to the 
  75. program. If this is not the last storage block, then another storage block 
  76. follows the allocated memory area.
  77.  When the storage block contains zero for the number of allocated segments, 
  78. then no storage is allocated to this block and the next storage block 
  79. immediately follows this one. This can happen when memory is allocated and 
  80. then deallocated repeatedly.
  81.  IBMDOS constructs a storage block and PSP before loading the command 
  82. interpreter (default is COMMAND.COM).
  83.  
  84.  If the copy of COMMAND.COM is a secondary copy, it will lack an environment 
  85. address at PSP+2Ch.
  86.  
  87.  
  88.  
  89. THE DISK TRANSFER AREA (DTA)├──────────────────────────────────────────────────
  90.  
  91.  DOS uses an area in memory to contain the data for all file reads and writes 
  92. that are performed with FCB function calls. This are is known as the disk 
  93. transfer area. This disk transfer area (DTA) is sometimes called a buffer. 
  94. It can be located anywhere in the data area of your application program and 
  95. should be set by your program.
  96.  
  97.  Only one DTA can be in effect at a time, so your program must tell DOS what 
  98. memory location to use before using any disk read or write functions. Use 
  99. function call 1Ah (Set Disk Transfer Address) to set the disk transfer address.
  100. Use function call 2Fh (Get Disk Transfer Address) to get the disk transfer 
  101. address. Once set, DOS continues to use that area for all disk operations until
  102. another function call 1Ah is issued to define a new DTA. When a program is given
  103. control by COMMAND.COM, a default DTA large enough to hold 128 bytes is 
  104. established at 80h into the program's Program Segment Prefix.
  105.  
  106.  For file reads and writes that are performed with the extended function calls,
  107. there is no need to set a DTA address. Instead, specify a buffer address when 
  108. you issue the read or write call.
  109.  
  110.  
  111. DOS PROGRAM SEGMENT├───────────────────────────────────────────────────────────
  112.  
  113.  When you enter an external command or call a program through the EXEC function 
  114. call, DOS determines the lowest availible address space to use as the start of 
  115. available memory for the program being started. This area is called the Program
  116. Segment.
  117.  At offset 0 within the program segment, DOS builds the Program Segment Prefix 
  118. control block. EXEC loads the program after the Program Segment Prefix (at
  119. offset 100h) and gives it control.
  120.  The program returns from EXEC by a jump to offset 0 in the Program Segment 
  121. Prefix, by issuing an int 20h, or by issuing an int 21h with register AH=00h or 
  122. 4Ch, or by calling location 50h in the PSP with AH=00h or 4Ch.
  123.  It is the responsibility of all programs to ensure that the CS register 
  124. contains the segment address of the Program Segment Prefix when terminating by
  125. any of these methods except call 4Ch.
  126.  
  127.  All of these methods result in returning to the program that issued the EXEC. 
  128. During this returning process, interrupt vectors 22h, 23h, and 24h (Terminate, 
  129. Ctrl-Break, and Critical Error Exit addresses) are restored from the values 
  130. saved in the PSP of the terminating program. Control is then given to the 
  131. terminate address.
  132.  
  133.  
  134. When a program receives control, the following conditions are in effect:
  135.  
  136. For all programs:
  137.  
  138. 1) The segment address of the passed environment is contained at offset 2Ch in 
  139.    the Program Segment Prefix.
  140.  
  141. 2) The environment is a series of ASCII strings totalling less than 32k bytes
  142.    in the form:       NAME=value      The default environment is 160 bytes.
  143.     Each string is a maximum of 127 bytes terminated by a byte of zeroes for a 
  144.    total of 128 bytes, and the entire set of strings is terminated by another
  145.    byte of zeroes. Following the byte of zeroes that terminates the set of
  146.    environment string is a set of initial arguments passed to a program that
  147.    contains a word count followed by an ASCIIZ string. The ASCIIZ string
  148.    contains the drive, path, and filename.ext of the executable program.
  149.    Programs may use this area to determine where the program was loaded from.
  150.    The environment built by the command processor (and passed to all programs
  151.    it invokes) contains a COMSPEC=string at a minimum (the parameter on COMSPEC
  152.    is the path used by DOS to locate COMMAND.COM on disk). The last PATH and
  153.    PROMPT commands issued will also be in the environment, along with any 
  154.    environment strings entered through the SET command. 
  155.     The environment that you are passed is actually a copy of the invoking 
  156.    process's environment. If your application terminates and stays resident 
  157.    through int 27h, you should be aware that the copy of the environment passed 
  158.    to you is static. That is, it will not change even if subsequent PATH,
  159.    PROMPT, or SET commands are issued.
  160.     The size of the environment may be changed from its default of 160 bytes
  161.    by using the SHELL= command in the config.sys from in DOS version 3.1 up,
  162.    or COMMAND.COM may be patched in earlier versions.
  163.  
  164.    The environment can be used to transfer information between processes or to
  165.    store strings for later use by application programs. The environment is
  166.    always located on a paragraph boundary. This is its format:
  167.         byte    ASCIIZ string 1
  168.         byte    ASCIIZ string 2
  169.             ....
  170.         byte    ASCIIZ string n
  171.         byte    of zeros (0)
  172.    Typically the environment strings have the form:
  173.         NAME = VALUE
  174.    The length of NAME or VALUE can be anything desired as long as it still fits
  175.    into the 123 byte space (4 bytes are used by "SET ").
  176.    Following the byte of zeros in the environment, a WORD indicates the number 
  177.    of other strings following. 
  178.  
  179.    If the environment is part of an EXECed command interpreter, it is followed 
  180.    by a copy of the DS:DX filename passed to the child process. A zero value 
  181.    causes the newly created process to inherit the parent's environment.
  182.  
  183. 3) Offset 80h in the PSP contains code to invoke the DOS function dispatcher.
  184.    Thus, by placing the desired function number in AH, a program can issue a
  185.    long call to PSP+50h to invoke a DOS function rather than issuing an int 21h.
  186.  
  187. 4) The disk transfer address (DTA) is set to 80h (default DTA in PSP).
  188.  
  189. 5) File Control Blocks 5Ch and 6Ch are formatted from the first two parameters 
  190.    entered when the command was invoked. Note that if either parameter contained
  191.    a path name, then the corresponding FCB will contain only a valid drive
  192.    number. The filename field will not be valid.
  193.  
  194. 6) An unformatted parameter area at 81h contains all the characters entered
  195.    after the command name (including leading and imbedded delimiters), with 80h
  196.    set to the number of characters. If the <, >, or | parameters were entered
  197.    on the command line, they (and the filenames associated with them) will not
  198.    appear in this area, because redirection of standard input and output is
  199.    transparent to applications.
  200.  
  201. (For EXE files only)
  202. 7) DS and ES registers are set to point to the PSP.
  203.  
  204. 8) CS, IP, SS, and SP registers are set to the values passed by the linker.
  205.  
  206. (For COM files only)
  207. 9) For COM files, offset 6 (one word) contains the number of bytes availible in 
  208.    the segment.
  209.  
  210. 10) Register AX reflects the validity of drive specifiers entered with the
  211.     first two parameters as follows:
  212.         AL=0FFh is the first parameter contained an invalid drive specifier,
  213.                 otherwise AL=00h.
  214.         AL=0FFh if the second parameter contained an invalid drive specifier, 
  215.                 otherwise AL=00h.
  216.  
  217. 11) All four segment registers contain the segment address of the inital 
  218.     allocation block, that starts within the PSP control block. All of user
  219.     memory is allocated to the program. If the program needs to invoke another
  220.     program through the EXEC function call (4Bh), it must first free some memory
  221.     through the SETBLOCK function call to provide space for the program being
  222.     invoked.
  223.  
  224. 12) The Instruction Pointer (IP) is set to 100h.
  225.  
  226. 13) The SP register is set to the end of the program's segment. The segment size
  227.     at offset 6 is rounded down to the paragraph size.
  228.  
  229. 14) A word of zeroes is placed on top of the stack.
  230.  
  231.  
  232.  The PSP (with offsets in hexadecimal) is formatted as follows:
  233.  (* = undocumented)
  234.  
  235. ┌──────────────────────────────────────────────────────────────────────────────┐
  236. │     P  R  O  G  R  A  M       S  E  G  M  E  N  T       P  R  E  F  I  X     │
  237. ├───────┬──────────┬───────────────────────────────────────────────────────────┤
  238. │ offset│   size   │                     C O N T E N T S                       │
  239. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  240. │ 0000h │ 2 bytes  │ int 20h                                                   │
  241. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  242. │ 0002h │ 2 bytes  │ segment address, end of allocation block                  │
  243. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  244. │ 0004h │ 1 byte   │ reserved, normally 0                                      │
  245. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  246. │ 0005h │ 5 bytes  │ FAR call to MSDOS function dispatcher (int 21h)           │
  247. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  248. │ 000Ah │ 4 bytes  │ previous termination handler interrupt vector (int 22h)   │
  249. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  250. │ 000Eh │ 4 bytes  │ previous contents of ctrl-C interrupt vector (int 23h)    │
  251. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  252. │ 0012h │ 4 bytes  │ prev. critical error handler interrupt vector (int 24h)   │
  253. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  254. │ 0016h │ 22 bytes │ reserved for DOS                                          │
  255. └───────┼──────────┼───────────────────────────────────────────────────────────┤
  256.       * │ 2 bytes  │ (16) parent process' PSP                                  │
  257.       * │ 20 bytes │ (18) "handle table" used for redirection of files         │
  258. ┌───────┼──────────┼───────────────────────────────────────────────────────────┤
  259. │ 002Ch │ 2 bytes  │ segment address of the program's environment block        │
  260. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  261. │ 002Eh │ 34 bytes │ reserved, DOS work area                                   │
  262. └───────┼──────────┼───────────────────────────────────────────────────────────┤
  263.       * │  4 bytes │ (2E) stores the calling process's stack pointer when      │
  264.         │          │      switching to DOS's internal stack.                   │
  265.       * │          │ (32) DOS 3.x max open files                               │
  266.       * │  2 bytes │ (3A) size of handle table   |these functions are in here  │
  267.       * │  4 bytes │ (3C) handle table address   |but reported addresses vary  │
  268. ┌───────┼──────────┼───────────────────────────────────────────────────────────┤
  269. │ 0050h │  3 bytes │ int 21h, RETF instruction                                 │
  270. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  271. │ 0053h │  2 bytes │ reserved - unused?                                        │
  272. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  273. │ 0055h │  7 bytes │ reserved, or FCB#1 extension                              │
  274. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  275. │ 005Ch │ 16 bytes │ default unopened File Control Block #1                    │
  276. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  277. │ 006Ch │ 16 bytes │ default unopened FCB #2 (overlaid if FCB #1 opened)       │
  278. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  279. │ 0080h │  1 byte  │ parameter length (number of chars entered after filename) │
  280. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  281. │ 0081h │   ...    │ parameters                                                │
  282. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  283. │ 00FFh │ 128 bytes│ command tail and default Disk Transfer Area (DTA)         │
  284. └───────┴──────────┴───────────────────────────────────────────────────────────┘
  285.  
  286.  
  287. 1. The first segment of availible memory is in segment (paragraph) form. For 
  288.    example, 1000h would respresent 64k.
  289.  
  290. 2. Offset 2Ch contains the segment address of the environment.
  291.  
  292. 3. Programs must not alter any part of the PSP below offset 5Ch.
  293.  
  294.  
  295. PSP (comments):
  296.  
  297. offset 00h  contains hex bytes CD 20, the int 20h opcode. A program can end
  298.             by making a jump to this location when the CS points to the PSP.
  299.             For normal cases, int 21, function 4Ch should be used.
  300.  
  301. offset 02h  contains the segment-paragraph address of the end of memory as 
  302.             reported by DOS. (which may not be the same as the real end of RAM).
  303.             Multiply this number by 10h or 16 to get the amount of memory
  304.             availible. ex. 1000h would be 64k.
  305.  
  306. offset 04h  "reserved or used by DOS" according to Microsoft
  307.  
  308. offset 05h  contains a long call to the DOS function dispatcher. Programs may 
  309.             jump to this address instead of calling int 21 if they wish. 
  310.             Used by Basic and other CPM object-code translated programs. It is
  311.             slower than standard int 21h.
  312.  
  313. offset 0Ah, 0Eh, 12h
  314.             vectors (IP, CS)
  315.  
  316. offset 16h  PSP:16h is the segment address of the invoking program's PSP, which
  317.         *   will most often be COMMAND.COM but perhaps may be a secondary
  318.             non-permanent COMMAND or a multitasking shell, etc. At any rate,
  319.             the resident shell version of COMMAND.COM has PSP:16H = PSP, which
  320.             indicates "don't look any lower in memory" for the command
  321.             interpreter. To find the beginning of the allocation chain, look
  322.             backwards through the PSP link addresses until the link address is
  323.             equal to the PSP segment address that it resides in. This should
  324.             be COMMAND.COM. To find COMMAND.COM's environment, look at the word
  325.             stored at offset 0BD3h (PC-DOS 3.1 only). This is a segment
  326.             address, so look there at offset 0.
  327.  
  328.        18h  handle alias table (networking). Also you can make PRN go to CON,
  329.         *   CON go to PRN, ERR go to PRN, etc. 0FFh = availible.
  330.  
  331. offset 2Ch  is the segment:offset address of the environment for the program 
  332.             using this particular PSP. This pointer does not point to
  333.             COMMAND.COM's environment unless it is a second copy of COMMAND.
  334.  
  335. offset 2Eh  the DWORD at PSP+2Eh is used by DOS to store the calling process's
  336.         *   stack pointer when switching to DOS's own private stack - at the end
  337.             of a DOS function call, SS:SP is restored from this address.
  338.  
  339.        32h, 34h
  340.         *   table of number of file handles (to 64k of handles!)
  341.  
  342. offset 40h  2 byte field points to the segment address of COMMAND.COM's PSP in
  343.         *   "weird" EXE files produced by Digital Research RASMPC/LINKPC.
  344.             EXE files created with these tools can cause all sorts of problems
  345.             with standard MSDOS debugging tools.
  346.  
  347. offset 50h  contains a long call to the DOS int 21 function dispatcher.
  348.  
  349. offset 5Ch, 65h, 6Ch
  350.             contain FCB information for use with FCB function calls. The first
  351.             FCB may overlay the second if it is an extended call; your program
  352.             should revector these areas to a safe place if you intend to use
  353.             them.
  354.  
  355. offset 5Ch  16 bytes first command-line argument (formatted as uppercase 11
  356.             character filename)
  357.  
  358. offset 6Ch  16 bytes second command-line argument (formatted as uppercase 11
  359.             character filename)
  360.  
  361. offset 7Ch-7Fh
  362.            "reserved or used by DOS"
  363. offset 80h  1 byte number of bytes in command line argument
  364.  
  365. offset 80h, 81h
  366.             contain the length and value of parameters passed on the command
  367.             line. 
  368.  
  369. offset 81h  97 bytes unformatted command line and/or default DTA
  370.  
  371. offset 0FFh contains the DTA
  372.           
  373.  
  374.  The PSP is created by DOS for all programs and contains most of the information
  375. you need to know about a program running. You can change the environment for
  376. the current process, however, but for the parent process, DOS in this case, you
  377. need to literally backtrack to DOS or COMMAND.COM's PSP. In order to get there
  378. you must look at the current PSP. At offset 16h of the current PSP segment,
  379. there a 2 byte segment address to the parent or previous process PSP.
  380.  From there you can manipulate the enviroment by looking at offset 2Ch. As you
  381. know, at offset 2Ch, there is 2 byte segment address to the environment block.
  382.  
  383. Try this under debug and explore the addresses located at these offsets;
  384.  
  385.       offset  length                description
  386.      ------------------------------------------------------------
  387.         16h     2       segment address of parent process PSP
  388.         2Ch     2       segment address of environment block.
  389.  
  390. Remember under debug you will have to backtrack two times.
  391.  
  392.         Programs        Parent
  393.       --------------------------
  394.         command.com     none
  395.         debug.com       command.com
  396.         program         debug.com
  397.  
  398.  
  399.  
  400.  
  401. MEMORY CONTROL BLOCKS├─────────────────────────────────────────────────────────
  402.  
  403.  DOS keeps track of allocated and availible memory blocks, and provides four
  404. function calls for application programs to communicate their memory needs to 
  405. DOS. These calls are:
  406.               48h --- allocate memory                 (MALLOC)
  407.               49h --- free allocated memory
  408.               4Ah --- modify allocated memory blocks  (SETBLOCK)
  409.               4Bh --- load or execute program         (EXEC)
  410.  
  411. DOS manages memory as follows:
  412.  
  413.  DOS build a control block for each block of memory, whether free or allocated.
  414. For example, if a program issues an "allocate" (48h), DOS locates a block of
  415. free memory that satisfies the request, and then "carves" the requested memory 
  416. out of that block. The requesting program is passed the location of the first 
  417. byte of the block that was allocated for it - a memory management control block,
  418. describing the allocated block, has been built for the allocated block and a 
  419. second memory management control block describes the amount of space left in the
  420. original free block of memory. When you do a SETBLOCK to shrink an allocated 
  421. block, DOS builds a memory management control block for the area being freed and
  422. adds it to the chain of control blocks. Thus, any program that changed memory 
  423. that is not allocated to it stands a chance of destroying a DOS memory 
  424. management control block. This causes unpredictable results that don't show up 
  425. until an activity is performed where DOS uses its chain of control blocks. The 
  426. normal result is a memory allocation error, which means a system reset will be 
  427. required.
  428.  
  429.  When a program (command or application program) is to be loaded, DOS uses the 
  430. EXEC function call 4Bh to perform the loading.
  431.  
  432.  This is the same function call that is availible to applications programs for 
  433. loading other programs. This function call has two options:
  434.  
  435.       Function 00h, to load and execute a program (this is what the command
  436.                     processor uses to load and execute external commands)
  437.  
  438.       Function 03h, to load an overlay (program) without executing it.
  439.  
  440.  Although both functions perform their loading in the same way (relocation is 
  441. performed for EXE files) their handling of memory management is different.
  442.  
  443. FUNCTION 0: For function 0 to load and execute a program, EXEC first allocates 
  444. the largest availible block of memory (the new program's PSP will be at offset 
  445. 0 in that block). Then EXEC loads the program. Thus, in most cases, the new 
  446. program owns all the memory from its PSP to the end of memory, including memory
  447. occupied by the transient parent of COMMAND.COM. If the program were to issue 
  448. its own EXEC function call to load and execute another program, the request 
  449. would fail because no availible memory exists to load the new program into.
  450.  
  451. NOTE: For EXE programs, the amount of memory allocated is the size of the 
  452.       program's memory image plus the value in the MAX_ALLOC field of the file's
  453.       header (offset 0Ch, if that much memory is availible. If not, EXEC 
  454.       allocates the size of the program's memory image plus the value in the 
  455.       MIN_ALLOC field in the header (offset 0Ah). These fields are set by the 
  456.       Linker).
  457.  
  458.  A well-behaved program uses the SETBLOCK function call when it receives 
  459. control, to shrink its allocated memory block down to the size it really needs.
  460. A COM program should remember to set up its own stack before doing the SETBLOCK,
  461. since it is likely that the default stack supplied by DOS lies in the area of 
  462. memory being used. This frees unneeded memory, which can be used for loading 
  463. other programs.
  464.  
  465.  If the program requires additional memory during processing, it can obtain 
  466. the memory using the allocate function call and later free it using the free 
  467. memory function call.
  468.  
  469.  When a program is loaded using EXEC function call 00h exits, its initial 
  470. allocation block (the block beginning with its PSP) is automatically freed 
  471. before the calling program regains control. It is the responsibility of all 
  472. programs to free any memory they allocate before exiting to the calling 
  473. program.
  474.  
  475.  FUNCTION 3: For function 3, to load an overlay, no PSP is built and EXEC 
  476. assumes the calling program has already allocated memory to load the new program
  477. into - it will NOT allocate memory for it. Thus the calling program should 
  478. either allow for the loading of overlays when it determines the amount of memory
  479. to keep when issuing the SETBLOCK call, or should initially free as much memory 
  480. as possible. The calling program should then allocate a block (based on the size
  481. of the program to be loaded) to hold the program that will be loaded using the 
  482. "load overlay" call. Note that "load overlay" does not check to see if the 
  483. calling program actually owns the memory block it has been instructed to load 
  484. into - it assumes the calling program has followed the rules. If the calling 
  485. program does not own the memory into which the overlay is being loaded, there is
  486. a chance the program being loaded will overlay one of the control blocks that 
  487. DOS uses to keep track of memory blocks.
  488.  
  489.  Programs loaded using function 3 should not issue any SETBLOCK calls since
  490. they don't own the memory they are operating in. (This memory is owned by the
  491. calling program)
  492.  
  493.  Because programs loaded using function 3 are given control directly by (and 
  494. return contrrol directly to) the calling program, no memory is automatically 
  495. freed when the called program exits. It is up to the calling program to 
  496. determine the disposition of the memory that had been occupied by the exiting 
  497. program. Note that if the exiting program had itself allocated any memory, it 
  498. is responsible for freeing that memory before exiting.
  499.  
  500.  Memory control blocks, sometimes called "arena headers" after their UNIX 
  501. counterpart, are 16 bytes long. Only the first 5 bytes are used. 16 bytes are
  502. used for the memory control block, which always starts at a paragraph boundary.
  503. When DOS call 48h is made to allocate "x" many paragraphs of memory, the amount
  504. used up is actually one more than the figure in the BX register to provide
  505. space for the associated memory control block. The location of the memory
  506. control block is at the paragraph immediately before the segment value returned
  507. in AX by the DOS function 48h call i.e. ((AX-1):0).
  508.  
  509. ┌──────────────────────────────────────────────────────────────────────────────┐
  510. │               M E M O R Y      C O N T R O L       B L O C K                 │
  511. ├───────┬──────────────────────────────────────────────────────────────────────┤
  512. │ Bytes │                           Function                                   │
  513. ├───────┼──────────────────────────────────────────────────────────────────────┤
  514. │   0   │ ASCII M or Z                                                         │
  515. ├───────┼──────────────────────────────────────────────────────────────────────┤
  516. │  1-2  │ PSP segment address of the program that owns this block of memory    │
  517. ├───────┼──────────────────────────────────────────────────────────────────────┤
  518. │  3-4  │ Size of next MCB in 16-byte paragraphs                               │
  519. ├───────┼──────────────────────────────────────────────────────────────────────┤
  520. │  5-F  │ unused                                                               │
  521. └───────┴──────────────────────────────────────────────────────────────────────┘
  522.  
  523. byte 1    will always have the value of 4Dh or 5Ah. The value 5Ah (Z) indicates
  524.           the block is the last in a chain, all memory above it is unused. 4Dh
  525.           (M) means that the block is intermediate in a chain, the memory above
  526.           it belongs to the next program or to DOS.
  527.  
  528. byte 2,3  hold the PSP segment address of the program that owns the
  529.           corresponding block of memory. A value of 0 means the block is free
  530.           to be claimed, any other value represents a segment address.
  531.  
  532. byte 3, 4 indicate the size in paragraphs of the memory block. If you know the
  533.           address of the first block, you can find the next block by adding the
  534.           length of the memory block plus 1 to the segment address of the
  535.           control block. Finding the first block can be difficult, as this
  536.           varies according to the DOS version and the configuration. 
  537.  
  538.  The remaining 11 bytes are not currently used by DOS, and may contain "trash" 
  539. characters left in memory from previous applications.
  540.  
  541.  If DOS determines that the allocation chain of memory control blocks has been 
  542. corrupted, it will halt the system and display the message "Memory Allocation 
  543. Error", and the system will halt, requiring a reboot.
  544.  
  545.  Each memory block consists of a signature byte (4Dh or 5Ah) then a word which
  546. is the PSP value of the owner of the block (which allocated it), followed by a 
  547. word which is the size in paragraphs of the block. The last block has a 
  548. signature of 5Ah. All others have 4Dh. If the owner is 0000 then the block is 
  549. free.
  550.  
  551.  Once a memory control block has been created it should only be manipulated
  552. with the appropriate DOS function calls. Accidentally writing over any of the
  553. first 5 bytes of a memory control block can cause a memory allocation error 
  554. and cause the system to lock up. If the first byte is overwritten with
  555. something other than an 'M' or a 'Z' then DOS will complain with an error
  556. return code of 7 signifying "Memory Control Blocks destroyed". However, should
  557. you change the ownership or block size bytes, you've had it.
  558.  
  559.  When a .COM program is first loaded by DOS and given control, the memory
  560. control block immediately preceding the Program Segment Prefix contains the
  561. following data:
  562.  
  563.           ID    = 'Z'
  564.           Owner = segment address of PSP (= CS register of .COM program)
  565.           Size  = number of available paragraphs in DOS memory pool
  566.  
  567.   An .EXE file will have the following data in the memory control block for
  568. the program (just prior to the PSP):
  569.  
  570.           ID    = 'M'
  571.           Owner = segment address of PSP (= DS register of program)
  572.           Size  = the number of paragraphs allocated to the program according
  573.                   to the information in the .EXE program header
  574.  
  575.  In the case of an .EXE program file the amount of memory allocated depends
  576. on the contents of the program header which informs the DOS loader how much to
  577. allocate for each of the segments in the program. With an .EXE program file
  578. there will always be a 'Z' memory control block created in memory immediately
  579. after the end of the space allocated to the program itself.
  580.  
  581.  One important fact to remember about DOS memory allocation is that blocks of
  582. RAM allocated by different calls to DOS function 48H will NOT be contiguous. At
  583. the very best, they will be separated by the 16 bytes of the memory control
  584. block, and at worst they could be anywhere in RAM that DOS manages to find a
  585. existing memory control block of sufficient size to accomodate the memory
  586. request.
  587.  
  588.  DOS treats the memory control blocks as a kind of linked list (term used
  589. loosely). It uses the earlier MCBs to find the later ones by calculating the
  590. location of the next one from the size of the prior one. As such, erasing any
  591. of the MCB data in the chain of MCBs will upset DOS severely, as each call for
  592. a new memory allocation causes DOS to scan the whole chain of MCBs looking for
  593. a free one that is large enough to fulfill the request.
  594.  
  595.  A separate MCB is created for the DOS environment strings at each program
  596. load, so there will be many copies of the environment strewn through memory
  597. when you have a lot of memory resident programs loaded. The memory control
  598. blocks for the DOS environment strings are not returned to the DOS memory pool
  599. if the program goes resident, as DOS will need to copy this enviroment for the
  600. next program loaded.
  601.  
  602.